package com.ccys.cms.service.userManagement.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.userManagement.DoctorAcronymsService;
import com.ccys.common.util.Kit;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.userManagement.DoctorAcronymsDto;
import com.ccys.db.entity.TblDoctorAcronyms;
import com.ccys.db.mapper.TblDoctorAcronymsMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.userManagement.DoctorAcronymsPageQueryVo;
import com.ccys.db.vo.userManagement.DoctorPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 医生短语设置接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 09:34
 **/

@Service
public class DoctorAcronymsServiceImpl extends BaseServiceImpl<TblDoctorAcronyms> implements DoctorAcronymsService {

    @Resource
    private TblDoctorAcronymsMapper tblDoctorAcronymsMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDoctorAcronyms(DoctorAcronymsDto dto) {
        //校验必传字段
        Kit.allParamValid(dto, "groupId", "acronym", "phrase");
        //直接写死 - 1-后台创建
        dto.setIsDefault(1);
        if(StringUtil.isNotEmpty(dto.getId())) {
            if (dto.getStatus() != null) {
                //保存状态修改日志
                baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            }
        }
        //新增/修改 - 并直接判断并返回
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDoctorAcronyms(DoctorAcronymsDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblDoctorAcronyms tblDoctorAcronyms = new TblDoctorAcronyms();
        tblDoctorAcronyms.setId(dto.getId());
        tblDoctorAcronyms.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblDoctorAcronymsMapper.updateByPrimaryKeySelective(tblDoctorAcronyms) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDoctorAcronymsStatus(DoctorAcronymsDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblDoctorAcronyms tblDoctorAcronyms = new TblDoctorAcronyms();
        tblDoctorAcronyms.setId(dto.getId());
        tblDoctorAcronyms.setStatus(dto.getStatus());
        if (tblDoctorAcronymsMapper.updateByPrimaryKeySelective(tblDoctorAcronyms) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<DoctorAcronymsPageQueryVo>, Object> findPageDoctorAcronyms(DoctorAcronymsDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DoctorAcronymsPageQueryVo> list = tblDoctorAcronymsMapper.findPageDoctorAcronyms(dto);
        PageInfo<DoctorAcronymsPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }
}
